#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QStackedWidget>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusInterface>
#include <QObject>
#include <QMap>
#include <QThread>
#include <QProcess>
#include <QApplication>
#include <QScreen>
#include <QMetaType>
#include <QMessageBox>
#include <kballontip.h>

#include "connectionservice.h"
#include "wifimanager.h"
#include "ftpmanager.h"
#include "filesyncmanage.h"
#include "usbmanage.h"
#include "devicemanage.h"
#include "pcscreenmanage.h"
#include "classificationlist/mobilefilesecondwidget.h"
#include "classificationlist/mobilefilewidget.h"
#include "connectinterface/connectinterface.h"
#include "connectinterface/searchdevicewin.h"
#include "connectinterface/devicecodewidget.h"
#include "storagelist/mobilestoragewidget.h"
#include "storagelist/copytocomputerbuttonwidget.h"
#include "view/titlebar.h"
#include "view/homepage.h"
#include "view/mobilefilelist.h"
#include "view/transmissiondialog.h"
#include "loadanimation.h"
#include "view/loaddialog.h"
#include "view/connectedwin.h"
#include "suspendtabbar.h"
#include "discovery.h"
#include "fineftp/server.h"
#include "kaboutdialog.h"
#include "dirmanager.h"
#include "publicattributes.hpp"

#define KYLIN_CONNECTIVITY_SEND_SERVICE "com.kylin.connectivity.send"
#define KYLIN_CONNECTIVITY_SEND_PATH "/com/kylin/connectivity"
#define KYLIN_CONNECTIVITY_SEND_INTERFACE "com.kylin.connectivity.send"

const QString MOUNT_DIR = "/var/lib/mult";

using namespace KMobileProjection;
using namespace fineftp;

class MainWindow : public QWidget
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", KYLIN_CONNECTIVITY_SEND_INTERFACE)
public:
    static MainWindow *getInstance();

    // 提供给dbus的接口
    // 获取当前是否有设备连接
    QString getDeviceName();
    // 获取当前目录文件列表
    void list(QString path);
    // 下载
    void downFile(const QList<FileInfo> &fileList, QString downloadPath);

protected:
    void closeEvent(QCloseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    enum WinIndex {
        WinHomePage = 0, // 首页
        WinConnect,      // 连接页面
        WinConnected,    // 已连接页面
    };

    enum WinFlage {
        DisconnectWin = 0, // 未连接
        SyncMainWin,       // 文件同步首页
        SyncQQWechatWin,   // qq微信文件同步页面
        MultFileListWin,   // 分类文件列表
        FileListWin,       // 存储文件列表
    };

    enum TYPE {
        None = 0,
        Mobile, // 手机连接
        Search, // 搜索
        Pctopc, // Pc连接
    };

    void initService();
    void initGsetting();
    void initDbus();
    void disconnectScreen();
    void disconnectFtp();
    void stopAllDown();
    void disconnectService();
    void deleterService();
    bool mountFtp(QString url, QString userName, QString pwd, QString path);
    bool umountFtp(QString path);
    void clearConnectionInfo();
    void changeLightTheme();
    void changeDarkTheme();
    void initUI();
    void connectUI();
    void updateResult();
    void initTransmissionDialog();
    void deleteMobileListWidget();
    void initMobileListWidget(int tag);
    void setToolTipWin(QString msg, kdk::TipType type);
    void uploadLocalListPath(QString itemName, const QStringList &localPath);
    void initMobileConnection();
    void initMobileFileSync();
    void setScreenOption();
    void deleteTransmissionDialog();
    void deleteStackedWidget();
    int getScreenIndex();
    void connectMobileFileWidget();
    void deleterUI();
    void startLoadUI();
    void startTimer();
    void stopLoadUI();
    void updateCurrentPath(int text);
    void initPcSearchInfo();
    void discInit();
    void deInitDisc();
    void deleteSuspendTab();
    void connectMobileFileList();
    void connectMobileStorage();

private:
    ConnectionService *m_connectionService = nullptr;   // 连接服务
    KMobileDiscovery::Discovery *m_discovery = nullptr; // 搜索手机设备信息
    FtpManager *m_ftpClient = nullptr;                  // ftp服务器
    DeviceManage *m_deviceManage = nullptr;             // 连接手机投屏
    UsbManage *m_usbManage = nullptr;                   // 监听USB插拔信号
    WifiManager *m_wifiManager = nullptr;               // 监听wifi信号
    QThread *m_wifiManagerThread = nullptr;             // 另起线程，监听wifi信号
    FileSyncManage *m_fileSyncManage = nullptr;         // 文件同步
    FtpServer *m_ftpServer = nullptr;
    PcScreenManage *m_pcScreen = nullptr;      // PC投屏
    QDBusInterface *m_dbusInterface = nullptr; // 挂载ftpdubs
    DirManager *m_dirManager = nullptr;        // 本地用户目录西信息管理

    bool m_isConnect = false;                                      // 当前是否连接
    QString m_currentPath = "";                                    // 手机存储当前路径
    int m_downFileType = FileSyncManage::LIST_TYPE::TYPE_FILE_NUM; // 当前下载类型
    QUrl m_url;                                                    // 服务器地址
    ConnectionService::ConnectionInfo m_connectInfo;               // 当前连接设备信息
    bool m_isDbusOperation = false;                                // 当前操作是否为dbus调用
    bool m_isMountFtp = false;                                     // 是否挂载ftp
    QString m_wifiIp = "";                                         // 本机IP
    bool m_isServerConnect = false;                                // 服务是否连接
    QString m_uploadPath = "";                                     // 当前为上传
    bool m_isPcSearchInfo = false;                                 // 是否为PC数据库文件
    QString m_reconnectAddress = "";                               // 重连ip

    Titlebar *m_titlebar = nullptr;                             // 标题栏
    HomePage *m_homepage = nullptr;                             // 首页
    ConnectInterface *m_connectInterfaceWin = nullptr;          // 连接界面
    ConnectedWin *m_connectedWin = nullptr;                     // 已连接界面
    SearchDeviceWin *m_searchDeviceWin = nullptr;               // 搜索设备界面
    QStackedWidget *m_mainStackedWin = nullptr;                 // 主界面切换堆栈窗口
    MobileFileList *m_mobileFileList = nullptr;                 // 文件分类界面
    QStackedWidget *m_stackedMobileFileListWin = nullptr;       // 分类界面与文件信息界面的堆栈窗口
    kdk::KAboutDialog *m_aboutWindow = nullptr;                 //关于窗口
    MobileFileWidget *m_listItemWidget = nullptr;               // 文件信息展示界面
    MobileStorageWidget *m_mobileStorageWidget = nullptr;       // 手机存储界面
    TransmissionDialog *m_transmissionDialog = nullptr;         // 传输进度窗口
    MobileFileSecondWidget *m_mobileFileSecondWidget = nullptr; // QQ微信分类界面
    QMessageBox *m_messageBox = nullptr;                        // 请求提示框
    LoadDialog *m_loadDialog = nullptr;                         //加载动画
    LoadAnimation *m_loadAnimation = nullptr;                   // 打开/更新加载界面
    QTimer *m_timer = nullptr;                                  // 连接加载定时器，连接超时处理
    SuspendTabBar *m_suspendTabBar = nullptr;                   // 投屏控制Tab

    int m_fileSyncIndex = 2;                                          // 手机文件同步堆栈窗口索引
    QListView::ViewMode m_classificationMode = QListView::IconMode;   // 分类列表当前视图模式
    QListView::ViewMode m_storageMode = QListView::IconMode;          // 手机存储当前视图模式
    QString m_tipMesseage = "";                                       // 提示消息
    WinFlage m_winFlage = WinFlage::DisconnectWin;                    // 当前所在界面
    PublicAttributes::Theme m_theme = PublicAttributes::Theme::Light; // 主题
    QPoint m_Clickpos;
    bool m_bLeftClick = false;

    // disc
    QProcess *m_discServer = nullptr;
    QProcess *m_discClient = nullptr;

Q_SIGNALS:
    // 提供dbus服务
    void sigConnected(QString);
    void sigDisconnect();
    void sigFileInfoList(const QMap<QString, FileInfo> &);

    void sigClearSearchLine();
public Q_SLOTS:
    // 外部
    void setLocalPathList(const QStringList &list);

    // 服务
    void slotVolumeConnected(QString volumeName);
    void slotVolumeDisconnected(QString volumeName);
    void slotWifiConnected(QString wifiIP);
    void slotWifiDisConnected();
    void slotConnectRequest(QString deviceName, ConnectionService::DeviceType deviceType);
    void slotConnectDenied();
    void slotConnect(const ConnectionService::ConnectionInfo &connectInfo);
    void slotDisconnect();
    void slotNotFountApk();
    void slotConnectFailed();
    void slotFtpConnected();
    void slotCurrentDirectoryList(const QMap<QString, FileInfo> &filelist);
    void slotTransferProgress(qint64 readBytes, qint64 totalBytes);
    void slotDownFileFinish(QString flag, QString filepath);
    void slotDownAllFileFinish(QString flag);
    void slotDownTempFileFinish(QString filePath);
    void slotDownFileFail(QString fileName, QString flag);
    void slotDownFileFail(QString fileName);
    void slotMountError(bool isMount, QString error);
    void slotAllDeviceInfo(const QMap<QString, KMobileCommon::DiscoveryDeviceInfo> &map);
    void slotRequestReceived(PcScreenManage::ScreenMsg msg);

    // UI
    void slotAboutWinShow();
    void onRecvSearchResult(QMap<QString, FileInfo> searchResult);
    void slotUSBConnectOnClicked();
    void slotItemButtonClicked(int text);
    void slotCdMobileStorageDirectory(QString path);
    void slotContinueSearch();
    void slotDownThumb();
    void slotMenuFilePath(QString filePath);
    void slotSetLastWidget();
    void slotShowMobileFileListWidget();
    void slotLocalPathList(const QStringList &localPathList);
    void slotShowMobileFileSecondWidget(int indexPath);
    void slotShowMobileStorage();
    void slotChooseFilePath(QString chooseFilePath, const QList<FileInfo> &selectFileNameList);
    void slotAbortTransmission();
    void slotConnectBtnClicked();
    void slotSearchInitFail();
    void slotInitDiscServerResult(bool);
    void slotReturnHomePage();
    void slotConnectServiceUI(QString address);
    void slotConnectedWinBtnClicked(ConnectedWin::BtnType type);
    void slotExitMobileScreen();
    void slotExitPcScreen();
    void slotTabBtnClicked(SuspendTabBar::BtnType type);
};

#endif // MAINWINDOW_H
